<?php

namespace App\Http\Controllers;

use Redis;
use App\Column;

class IndexController extends CommonController
{
    public function index()
    {
        $pictureNews = unserialize(Redis::get('api:picture_news/3'));

        $columns = unserialize(Redis::get('api:columns/articles'));

        return ['picture_news' => $pictureNews, 'article_list' => $columns];

        // $pictureNews = $this->articleFieldExceptContent()
        //     ->where('article_havelogo', 1)
        //     ->latest('article_addtime')
        //     ->take(3)
        //     ->get();

        // $columns = $this->getColumn();

        // foreach ($columns as $column) {
        //     $column->articles = $this->fetchColumnArticle($column->column_id);
        // }

        // return ['picture_news' => $pictureNews, 'article_list' => $columns];
    }

    protected function fetchColumnArticle($columnId)
    {
        $articles = $this->fetchArticleByColumnId($columnId);

        if (empty($articles)) {
            $articles = $this->fetchArticleBySubColumnId($columnId);
        }

        return $articles;
    }

    protected function fetchArticleByColumnId($columnId)
    {
        return $this->articleFieldExceptContent()
            ->where('article_lanmu', '=', $columnId)
            ->latest('article_addtime')
            ->take(3)
            ->get();
    }

    protected function fetchArticleBySubColumnId($columnId)
    {
        $columnIdArr = Column::fetchColumnIdArr($columnId);

        return $this->articleFieldExceptContent()
            ->whereIn('article_lanmu', $columnIdArr)
            ->latest('article_addtime')
            ->take(3)
            ->get();
    }

    public function product()
    {
        echo Redis::get('api:product');
    }
}